package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

//Mapper是数据持久层的类，进行数据库操作
@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;
    public int deleteByUsername(String username){
        try (Connection connection=dbUtil.getConnection()) {
            String sql="DELETE FROM user " +
                    "WHERE username=? ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public List<User> selectAll(){
        List<User> list=new ArrayList<>();
        try (Connection connection=dbUtil.getConnection()){
            Statement statement=connection.createStatement();
            String sql="SELECT id,username,password,nickname,age " +
                    "FROM user";
            ResultSet rs=statement.executeQuery(sql);
            while (rs.next()){
                int id=rs.getInt("id");
                String username=rs.getString("username");
                String password=rs.getString("password");
                String nickname=rs.getString("nickname");
                int age=rs.getInt("age");
                User user=new User(id,username,password,nickname,age);
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public int updata(User user){
        System.out.println("UserMapper插入修改数据");
        try(Connection connection=dbUtil.getConnection()) {
            String sql="UPDATE user " +
                    "SET nickname=?,password=?,age=? " +
                    "WHERE username=? ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,user.getNickname());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
    public int insert(User user){
        System.out.println("UserMapper:插入用户数据");
        //连接诶数据库
        try (Connection conn= dbUtil.getConnection()){
           String sql="INSERT INTO user(username,password,nickname,age) " +
                   "VALUES(?,?,?,?)";
           PreparedStatement ps=conn.prepareStatement(sql);
           ps.setString(1, user.getUsername());
           ps.setString(2, user.getPassword());
           ps.setString(3, user.getNickname());
           ps.setInt(4, user.getAge());
           return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    public User selectByUserName(String username){
        try (Connection connection=dbUtil.getConnection()){
            String sql="SELECT id,username,password,nickname,age " +
                    "FROM user " +
                    "WHERE username=? ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                User user =new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public User selectByUserNameAndPassword(String username,String password){
        try (Connection connection=dbUtil.getConnection()){
            String sql="SELECT id,username,password,nickname,age " +
                    "FROM user " +
                     "WHERE username=? AND password=? ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                User user =new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
